 <template>
  	<view class="Page">
  		<view class="title">已扫码列表</view>
  		<view class="noData" v-show="list.length==0">当前无扫码数据</view>
  		<view v-show="list.length!=0" class="is_show_box">
  			<view>
  				<view class="data_item" v-for="(item ,i) in list" :key="i">
  						<view class="card-left">
  							<view><text class="gray">物料名称：</text>{{item.spare_manage__name}}</view>
  							<view><text class="gray">物料编码：</text>{{item.spare_manage__code}}</view>
  							<view style="font-weight:600 ;">库存数量({{item.spare_manage__unit__name}})：<text style="color: red;"> {{item.num}}</text></view>
  							<view><text class="gray">规格：</text>{{item.spare_manage__specs }} {{' '}}<text class="gray">型号：</text>{{item.spare_manage__model}}</view>
  							<view><text class="gray">库存属性：</text>{{item.now_spare_status__name }} {{' '}}<text class="gray">流水号：</text>{{item.no}}</view>
 							<view><text class="gray">库存位置：</text>{{list.store_place__flag}}</view>
  						</view>
  						<button size="mini" type="warn" @click="delete_fun(i)" style="font-size: 14px;">删除</button>
  				</view>
  			</view>	
  		</view>	
<!--  		<uni-popup ref="mabucunzai" type="message">
  			<uni-popup-message type="error" message="扫的码不是本系统的码" :duration="2000"></uni-popup-message>
  		</uni-popup>
  		<uni-popup ref="idbucunzai" type="message">
  			<uni-popup-message type="error" message="扫的码id不存在" :duration="2000"></uni-popup-message>
  		</uni-popup>
 		<uni-popup ref="kucun0" type="message">
 			<uni-popup-message type="error" message="该物料库存数量为0,无法修改状态" :duration="2000"></uni-popup-message>
 		</uni-popup>
  		<uni-popup ref="no_data" type="message">
  			<uni-popup-message type="error" message="请先扫码添加需修改状态物料" :duration="2000"></uni-popup-message>
  		</uni-popup> -->
 		<uni-popup ref="isUpdate" type="dialog">
 			<uni-popup-dialog type="success" cancelText="取消" confirmText="确定" title="通知" content="是否确定修改状态" @confirm="updateConfirm"
 				@close="isUpdateClose"></uni-popup-dialog>
 		</uni-popup>
<!--  		<uni-popup ref="success_update" type="message">
  			<uni-popup-message type="success" message="修改状态成功" :duration="2000"></uni-popup-message>
  		</uni-popup> -->
  		<view class="kongbai">
  		</view>
  	</view>
	<view class="choose_status">
		<label style="font-size: 14px;">选择状态：</label>
		<picker @change="bindPickerChange($event,statusList)" :value="index" :range="statusList" :range-key="'name'">
			<view class="uni-input" style="font-size: 14px;">{{statusList[index].name}}</view>
		</picker>
	</view>

 	<view class="buttonBox">
 		<button size="default" type="primary"  @click="update">修改状态</button>
 		<button size="default" type="default"  @click="scan1" class="scanButton">扫码</button>
 	</view>
  </template>
  
  <script>
  	export default {
  		data() {
  			return {
  				id:123,
  				code:1234,
  				// 二维码查询出的码
  				qr_code:"",
  				list:[],
  				return_data:[],
  				org_id:34,
				index: 0,
				statusList:[],
				choosedFlag:'',
  			}
  		},
 		computed:{
 			getNowFormatDate() {
 			  let date = new Date(),
 			    year = date.getFullYear(), //获取完整的年份(4位)
 			    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
 			    strDate = date.getDate() // 获取当前日(1-31)
 			  if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
 			  if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
 			 
 			  return `${year}-${month}-${strDate}`
 			}
 		},
  		onLoad() {
  			this.scan1();
			
			// 调用移库接口
			let that=this;
			dd.httpRequest({
			  url: 'http://113.15.231.21:180/storage_manage2/ding_app/query_spare_status_list',
			  method: 'POST',
			  headers: {      sessionid:that.$root.sessionID },
			  data: {
				org_id:34,               // 组织ID
				is_active: true,
			  },
			  dataType: 'json',
			  success: function(res){
				console.log('status接口');
				console.log(res);
				console.log(res.data.result.list);
				that.statusList=res.data.result.list;
				
				that.choosedFlag=that.statusList[0].flag;
				},
				fail: function(res) {
					console.log("fail")
				}
			});
  		},
  		methods: {
  // 			scan1(){
  // 				let that=this;
  // 				uni.scanCode({
  // 					scanType: ['qrCode'],
  // 					success: function (res) {
  // 						console.log(res.result)
  // 						let strArray=res.result.split(',')
  // 						console.log(strArray)
  // 						if(strArray[0][5]!='d'){
  // 							console.log('扫的码不正确')
  // 							that.$refs.mabucunzai.open('center')
  // 						}
  // 						else{
  // 							console.log('扫的码正确')
  // 							that.qr_code=strArray[0].slice(12);
  // 							that.org_id=strArray[2].slice(7);
  // 							console.log(that.qr_code);
  // 							console.log(that.org_id);
  // 							// console.log(that.qr_code)
  // 							// 发起网络请求
  // 							dd.httpRequest({
  // 							  url: 'http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
  // 							  method: 'POST',
		// 					  headers: {      sessionid:that.$root.sessionID },
  // 							  data: {
  // 								org_id:that.org_id, 
  // 								ids:that.qr_code, 
  // 							  },
  // 							  dataType: 'json',
  // 							  success: function(res){	
  // 								if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
  // 									console.log("存在");
  // 									console.log(res.data.result.list[0]);
 									
 	// 								if(res.data.result.list[0].num>0){
 	// 									that.list.push(res.data.result.list[0]);
 	// 									// 返回服务端的数组
 										
 	// 									that.return_data.push({
 	// 										spare_status_flag: 'new', // 库存状态
 	// 										spare_device_id: res.data.result.list[0].id,      // 流水码ID（单个实物ID）
 	// 										num: res.data.result.list[0].num,                  // 领料数量
 	// 									})
 	// 									console.log("r1");
 	// 									console.log(that.return_data);
 	// 									// 获取的去重
 	// 									let map = new Map();
 	// 									for (let item of that.list) {
 	// 										map.set(item.id, item);
 	// 									 }
 	// 									that.list = [...map.values()];
 	// 									console.log("c2");
 	// 									console.log(that.list);
 	// 									// 返回的去重
 	// 									let map1 = new Map();
 	// 									for (let item of that.return_data) {
 	// 										map1.set(item.spare_device_id, item);
 	// 									 }
 	// 									that.return_data = [...map1.values()];
 	// 									console.log("r2");
 	// 									console.log(that.return_data);
 										
 	// 								}
 	// 								else{
 	// 									that.$refs.kucun0.open('center')
 	// 								}
  
  // 								}
  // 								else{
  // 									console.log("不存在");
  // 									this.$refs.idbucunzai.open('center')
  // 								}
  // 							},
  // 							  fail: function(res) {
  // 								console.log("fail")
  // 								},
  // 							  complete: function(res) {
  // 								console.log("com")
  // 								}
  // 							});
  // 						}
  
  // 					}
  // 				});	
  				
  // 			},
  scan1(){
  				let that=this;
  				uni.scanCode({
  					scanType: ['qrCode'],
  					success: function (res) {
  						console.log(res.result)
  						let strArray=res.result.split(',')
  						console.log(strArray)
  						if(strArray[0][5]!='d'){
  							console.log('扫的码不正确')
  							// that.$refs.mabucunzai.open('center')
  							that.$root.message('fail','扫的码不是本系统的码')
  						}
  						else{
  							console.log('扫的码正确')
  							that.qr_code=strArray[0].slice(12);
  							that.org_id=strArray[2].slice(7);
  							console.log(that.qr_code);
  							console.log(that.org_id);
							that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
								{
									org_id:that.org_id, 
									ids:that.qr_code, 
								}, 
								{
									sessionid:that.$root.sessionID
								})
								.then((res) => {
									let fun=function(){
										if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
											console.log("存在");
											console.log(res.data.result.list[0]);
											
											if(res.data.result.list[0].num>0){
												that.list.push(res.data.result.list[0]);
												// 返回服务端的数组
												
												that.return_data.push({
													spare_status_flag: 'new', // 库存状态
													spare_device_id: res.data.result.list[0].id,      // 流水码ID（单个实物ID）
													num: res.data.result.list[0].num,                  // 领料数量
												})
												console.log("r1");
												console.log(that.return_data);
												// 获取的去重
												let map = new Map();
												for (let item of that.list) {
													map.set(item.id, item);
												 }
												that.list = [...map.values()];
												console.log("c2");
												console.log(that.list);
												// 返回的去重
												let map1 = new Map();
												for (let item of that.return_data) {
													map1.set(item.spare_device_id, item);
												 }
												that.return_data = [...map1.values()];
												console.log("r2");
												console.log(that.return_data);
												
											}
											else{
												// that.$refs.kucun0.open('center')
												that.$root.message('fail','该物料库存数量为0,无法移库')
											}
		 
										}
										else{
											console.log("不存在");
											// this.$refs.idbucunzai.open('center')
											that.$root.message('fail','扫的码id不存在')
										}
									}
									that.$root.judge(res,fun);
								})
  						}
  
  					}
  				});	
  				
  			},
  			delete_fun(i){
  				this.list.splice(i, 1);
  				this.return_data.splice(i, 1);
  				console.log('删除后的')
  				console.log(this.return_data);
  
  			},
			bindPickerChange: function(e,list) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.index = e.detail.value;
				this.choosedFlag=list[this.index].flag;
				console.log(list[this.index].name+list[this.index].flag)
			},
			update(){
				if(this.return_data.length>0&&this.list.length>0){
					this.$refs.isUpdate.open('center')
				}
				else{
					this.$refs.no_data.open('center')
				}
			},
 			updateConfirm(){
 				// 调用移库接口
 				let that=this;
 				// dd.httpRequest({
 				//   url: 'http://113.15.231.21:180/storage_manage2/ding_app/create_spare_record_by_no_change',
 				//   method: 'POST',
				 //  headers: {      sessionid:that.$root.sessionID },
 				//   data: {
 				// 	org_id:that.org_id,               // 组织ID
 				// 	record_type_flag: 'change',   // 业务类型，这里写死
 				//    // store_place_id:that.choosedID,       // 目标库位
				 //   spare_status_flag:that.choosedFlag,
 				//    date:that.getNowFormatDate,        // 领用时间，默认当天
 				//    data:JSON.stringify(that.return_data),
 				//   },
 				//   dataType: 'json',
 				//   success: function(res){
 				// 	that.list=[];
 				// 	that.return_data=[];
 				// 	that.$refs.success_update.open('center')
 				// 	},
 				// 	fail: function(res) {
 				// 		console.log("fail")
 				// 	},
 				// 	complete: function(res) {
 				// 		console.log("com")
 				// 	}
 				// });
				that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/create_spare_record_by_no_change',
					{
						org_id:that.org_id,               // 组织ID
						record_type_flag: 'change',   // 业务类型，这里写死
					   // store_place_id:that.choosedID,       // 目标库位
					   spare_status_flag:that.choosedFlag,
					   date:that.getNowFormatDate,        // 领用时间，默认当天
					   data:JSON.stringify(that.return_data),
					}, 
					{
					  	sessionid:that.$root.sessionID
					})
					.then((res) => {
						let fun=function(){
							that.list=[];
							that.return_data=[];
						}
						that.$root.judge(res,fun);
					})
 			},
 			
  		}
  	}
  </script>
  
  <style scoped>
  	.Page{
  		width: 100%;
  		font-size: 14px;
   		background-color: #F6F6F6;
  		display: flex;
  		flex-direction: column;
  		/* align-items: center; */
 		padding-left: 10px;
 		padding-right: 10px;
  		
  	}
  	.title{
  		font-size: 16px;
  		margin-top: 10px;
  		font-weight: 600;
		border-left: 2px solid dodgerblue;
		padding-left: 8px ;
  	}
  	.noData{
  		margin-top: 15px;
  	}
  	
  
  	.data_item{
  		margin-top: 12px;
  		width: 90%;
  		background-color: white;
  		border-radius: 5px;
  		font-size: 14px;
  		padding-left: 10px;
  		padding-bottom: 5px;
  		display: flex;
  		flex-direction: row;
  		justify-content: space-between;
  		
  	}
  	.card-left view{
  		margin-top: 5px;
  	}
  	.gray{
  		color: #93A1A1;
  	}
  	.num-icon{
  		width: 16px;
  		height: 16px;
  	}
  	.dialog_center{
  		display: flex;
  		flex-direction: row;
  		justify-content: center;
  		align-items: center;
  	}
  	.is_show_box{
  		width: 100%;
  	}
	.choose_status{
		position: fixed;
		bottom: 47px;
		width: 100%;
		height: 40px;
		background-color: white;
		line-height: 40px;
		display: flex;
		flex-direction:row;
		justify-content: flex-start;
		align-items: center;
	}
	.choose_status label{
		margin-left: 10px;
		width: 30%;
		
	}
	.uni-input{
		width: 63%;
		border: 1px solid gainsboro;
		height: 32px;
		line-height: 32px;
	} 
  	.kongbai{
  		width: 100%;
  		height: 60px;
  	}
  	.buttonBox{
  		width: 100%;
  		position: fixed;
  		bottom: 0%;
  		display: flex;
  		flex-direction: row;
  		justify-content: space-around;
  		align-items: center;
  	}
  	.buttonBox button{
  		width: 50%;
  	}
  	.scanButton{
  		background-color: #42B983;
  		/* background-color: #41A863; */
  		color: white;
  		border: 1px solid #42B983;
  	}
  </style>
  